﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using EHTWebsite.Code;

/// <summary>
/// Author toanvt
/// CreatedDate 07/12/2011
/// </summary>
public partial class Administrator_AdminCompanyImages : System.Web.UI.Page
{
    private Int32 pageIndex = 1;
    private Int32 pageSize = 10;
    private Int32 totalRecord;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
        }
        catch (Exception)
        {
            pageIndex = 1;
        }

        if (!Page.IsPostBack)
        {
            LoadDataHinhAnhCongTy();
        }
    }

    private void LoadDataHinhAnhCongTy()
    {
        DataSet dsHinhAnhCongTys = HinhAnhCongTyProvider.GetHinhAnhCongTysForBackEnd(pageIndex, pageSize, out totalRecord);

        if (dsHinhAnhCongTys.Tables[0].Rows.Count > 0)
        {
            this.rpHinhAnhs.DataSource = dsHinhAnhCongTys;
            this.rpHinhAnhs.DataBind();

            foreach (RepeaterItem item in rpHinhAnhs.Items)
            {
                CheckBox ckPublish = (CheckBox)item.FindControl("ckPublish");

                //Hiện biểu tượng tương ứng với trạng thái Publish
                if (ckPublish.Checked == true)
                {
                    ((ImageButton)item.FindControl("btnPublish")).ImageUrl = "Templates/Images/btncheck.png";
                }
                else
                {
                    ((ImageButton)item.FindControl("btnPublish")).ImageUrl = "Templates/Images/unpublish.jpg";
                }
            }

            PaggingControl1.PageURL = "AdminBanners.aspx";
            PaggingControl1.PageIndex = pageIndex;
            PaggingControl1.TotalRecord = totalRecord;
        }
    }

    protected void rpHinhAnhs_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        String cmdName = e.CommandName;
        Int32 hinhAnhID = -1;

        try
        {
            hinhAnhID = Int32.Parse(e.CommandArgument.ToString());
        }
        catch (Exception ex)
        {
        }

        if (cmdName == "delete1")
        {
            HinhAnhCongTyProvider.DeleteHinhAnhCongTyByHinhAnhID(hinhAnhID);
            LoadDataHinhAnhCongTy();
        }
        else if (cmdName == "publish")
        {
            HinhAnhCongTyProvider.ChangePublishByHinhAnhID(hinhAnhID);
            LoadDataHinhAnhCongTy();
        }
        else if (cmdName == "change_number_of_order")
        {
            try
            {
                Int32 numberOfOrder = Int32.Parse(((TextBox)e.Item.FindControl("txtNumberOfOrder")).Text.Trim());
                HinhAnhCongTyProvider.UpdateNumberOfOrderByHinhAnhID(hinhAnhID, numberOfOrder);
            }
            catch (Exception ex)
            {

            }

            LoadDataHinhAnhCongTy();

        }
        else if (cmdName == "save_all")
        {
            foreach (RepeaterItem item in this.rpHinhAnhs.Items)
            {
                Int32 numberOfOrder = Int32.Parse(((TextBox)item.FindControl("txtNumberOfOrder")).Text.Trim());
                hinhAnhID = Int32.Parse(((Literal)item.FindControl("ltrHinhAnhID")).Text.Trim());
                BannerProvider.UpdateNumberOfOrderByBannerID(hinhAnhID, numberOfOrder);
            }

            LoadDataHinhAnhCongTy();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("AdminCompanyImageIU.aspx");
    }
}
